package com.hay.oss;


import com.hay.oss.util.OssSpringUtil;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * oss功能模板
 * @author LiMouRen
 * @date 2024/4/11
 */
public class OssTemplateFactory {

    private static final Map<String, OssTemplate> BEAN_CACHE = new ConcurrentHashMap<>();

    public static OssTemplate getTemplate(String type) {
        OssTemplate service = BEAN_CACHE.get(type);
        if (Objects.nonNull(service)){
            return service;
        }
        Class<? extends OssTemplate> aClass = OssTypeEnum.getTemplateClass(type);
        synchronized (OssTemplateFactory.class){
            service = BEAN_CACHE.get(type);
            if (Objects.isNull(service)){
                service = OssSpringUtil.getBean(aClass);
                if (null == service) {
                    throw new RuntimeException("该功能模板不存在");
                }
                BEAN_CACHE.put(type,service);
            }
        }
        return service;
    }


}